from fastapi import APIRouter, Depends, status
from sqlalchemy.orm import Session
from typing import List, Optional

from config.Database import get_db
from schemas.Notification import NotificationCreate, NotificationUpdate, NotificationResponse
from schemas.Result import Result
from schemas.Submitinfo import SubmitInfoUpdate
from services import notification_service
from services.SubmitInfoServe import SubmitInfoServe

router = APIRouter(
    prefix="/notification",
    tags=["notification"],
    responses={404: {"description": "Not found"}},
)

# 创建通知
@router.post("/")
def create_notification(notification: NotificationCreate, db: Session = Depends(get_db)):
    SubmitInfoServe.update(db, 24, SubmitInfoUpdate(id=24, isPush=True))
    return Result.success("")
    # 检查ID是否已存在
    if notification_service.check_id_exists(db, notification.id):
        return Result.fail(msg="通知ID已存在")
    # 创建新通知
    db_notification = notification_service.create(db, notification)
    return Result.success(db_notification)

# 获取所有通知
@router.get("/")
def get_all_notifications(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    notifications = notification_service.get_all(db, skip=skip, limit=limit)
    return Result.success(
        data=notifications,
        msg="获取通知列表成功"
    )

# 根据ID获取通知
@router.get("/{notification_id}")
def get_notification(notification_id: int, db: Session = Depends(get_db)):
    notification = notification_service.get_by_id(db, notification_id)
    if notification is None:
        return Result.fail(msg="通知不存在", code=404)
    
    return Result.success(
        data=notification,
        msg="获取通知成功"
    )

# 更新通知
@router.put("/{notification_id}")
def update_notification(notification_id: int, notification: NotificationUpdate, db: Session = Depends(get_db)):
    updated_notification = notification_service.update(db, notification_id, notification)
    if updated_notification is None:
        return Result.fail(msg="通知不存在", code=404)
    
    return Result.success(
        data=updated_notification,
        msg="更新通知成功"
    )

# 删除通知（软删除）
@router.delete("/{notification_id}")
def delete_notification(notification_id: int, db: Session = Depends(get_db)):
    success = notification_service.delete(db, notification_id)
    if not success:
        return Result.fail(msg="通知不存在", code=404)
    
    return Result.success(msg="删除通知成功")

# 根据邮箱查询通知
@router.get("/email/{email}")
def get_notifications_by_email(email: str, db: Session = Depends(get_db)):
    notifications = notification_service.get_by_email(db, email)
    return Result.success(
        data=notifications,
        msg="获取通知列表成功"
    )